1 package com.inigoserrano.isvalidator.check;
2
3 import com.inigoserrano.isvalidator.data.Data;
4 import com.inigoserrano.isvalidator.data.DataInternal;
5 import com.inigoserrano.isvalidator.errorDo.CheckParameterProvider;
6 import com.inigoserrano.isvalidator.errorDo.ErrorDo;
7
8 /***
9 * This class is a base for the Constraints, implements the interface
10 * Constraint and extends it to add methods used internaly, so the Constraint
11 * interface represents the userīs view of the Constraint and this class
12 * representes the internal (developer) view
13 * @license@
14 * @version @version@
15 * @author @author@
16 **/
17 public abstract class SimpleCheck implements CheckParameterProvider, Check {
18 /***
19 * If the check dosnīt match it must throw an Exception or no
20 */
21 protected boolean throwException = false;
22 /***
23 * The container of the data
24 */
25 protected DataInternal container = null;
26 /***
27 * To indicate if the data match this Check
28 */
29 protected boolean match = false;
30 /***
31 * To indicate if this Check has been checked (and donīt do again)
32 */
33 protected boolean hasBeenChecked = false;
34 /***
35 * The String to check (taken from the container)
36 */
37 protected String valueToCheck = null;
38 /***
39 * this attribute say if in the constructor of the class is say what do
40 * with the thrown of exceptions, by default it takes the container value
41 */
42 protected boolean explicityThrowException = false;
43
44 /***
45 * This method tell to the constriant who is its container. The Constraint
46 * gets of its container the value to check (the data to check)
47 * @param container the ConstraintContainer in with this Constraint live
48 * @throws InternalCheckException If the container passed as parameter
49 * isnīt an instance of ConstraintContainerInternal
50 **/
51 public void addContainer(Data container) throws InternalCheckException {
52 if (container instanceof DataInternal) {
53 if (this.container == null) {
54 this.container = (DataInternal) container;
55 //Store it in the objectīs attribute
56 } else {
57 throw new InternalCheckException();
58 //If the container passed as parameter isnīt an instance of
59 // ConstraintContainerInternal, then its an error
60 }
61 }
62 this.valueToCheck = this.container.getValueToCheck();
63 //If in the constructor of the constraint it hasnīt say what to do
64 //with the Exception, then it takes the containerīs option
65 if (!this.explicityThrowException) {
66 this.throwException = this.container.isTrownException();
67 }
68 }
69
70 /***
71 * Return the identifier of the Check, it is unique. One Check has
72 * only One Identifier.
73 * Normaly it is the class name
74 * @return the identifier of the Check
75 **/
76 public abstract String getIdentifier();
77
78 /***
79 * This method adds the parameters of the constraint to one
80 * InvalidConstraintProcesor that is listen.
81 * This method adds the parameter "constraint" with the identifier
82 * of the constraint and the parameter "valueToCheck"
83 * with the value that has been checked (Usually this method is call
84 * when it has some invalid data)
85 * @param listener Class that receive the params of the check to process it
86 **/
87 public void errorDo(ErrorDo listener) {
88 //Allways the name (identifier) of the constraint
89 listener.addParameter("constraint", this.getIdentifier());
90 //Allways the value that has been checked
91 if (this.valueToCheck == null) {
92 listener.addParameter("valueToCheck", "null");
93 } else {
94 if (valueToCheck.equalsIgnoreCase("")) {
95 listener.addParameter("valueToCheck", "blank");
96 } else {
97 listener.addParameter("valueToCheck", this.valueToCheck);
98 }
99 }
100 }
101
102 /***
103 * This method implements the interfaceīs signature
104 * @return true if the constraint has been checked, else returns false
105 **/
106 public boolean isChecked() {
107 return this.hasBeenChecked;
108 }
109
110 /***
111 * This method implements the interfaceīs signature
112 * @return true if the data match the constraint, else returns false
113 **/
114 public boolean match() {
115 return this.match;
116 }
117 }
This page was automatically generated by Maven